﻿using System;
using System.Collections.Generic;
using System.Text;
using NLBS.Data;
using NLBS.Entity;
using NLBS.Data.Business;
using NLBS.Views;

namespace NLBS.Web.UI
{
   public class SmileCommand:ICommand,IAction<Smile>
    {
        #region Presenter

       private SmilePresenter _presenter;

        public SmilePresenter Presenter
        {
            get
            {
                if (this._presenter == null)
                    _presenter = new SmilePresenter();
                this._presenter.Action = this;
                return _presenter;
            }
        }

        private void PresenterInit()
        {
            this.Presenter.OnActionLoaded();
        }
        #endregion

        private Action act;
       private string[] parameters;
       public SmileCommand(Action act, string[] parameters)
       {
           this.act=act;
           this.parameters = parameters;
           
       }
        #region ICommand Members

        public void Excute()
        {
            PresenterInit();
            switch (act)
            {
                case Action.Insert:
                    int a = parameters[1].IndexOf(",");
                    if (a > 0)
                    {
                        string[] smileIDList = parameters[1].Split(new char[] { ',' });
                        string[] smileImage = parameters[2].Split(new char[] { ',' });
                        string[] smileCode = parameters[3].Split(new char[] { ',' });
                        for (int j = 0; j < smileIDList.Length; j++)
                        {
                            Smile updatesmile = new Smile();
                            updatesmile.SmID = Convert.ToInt32(smileIDList[j]);
                            updatesmile.Image = smileImage[j];
                            updatesmile.Code = smileCode[j];
                           SmileData.UpdateSmile(updatesmile);
                        }

                    }
                    if (parameters.Length > 4)
                    {
                        string[] newSmile = parameters[4].Split(new char[] { ',' });
                        Smile sm = new Smile();
                        sm.Code = newSmile[0];
                        sm.Image = newSmile[1];
                        SmileData.InsertSmile(sm);
                    }
                    break;
                case Action.Delete:
                    for (int i = 0; i < parameters.Length; i++)
                    {
                        if (i > 0)
                        {
                            SmileData.DeleteSmile(Convert.ToInt32(parameters[i]));
                        }
                    }
                    break;
               
            }
        }

        #endregion

        #region IAction<Smile> Members

        private Smile _entity;
        public Smile Entity
        {
            get
            {
                return _entity;
            }
            set
            {
                _entity = value;
            }
        }

        private int _keyID;
        public int keyID
        {
            get
            {
                return _keyID;
            }
            set
            {
                _keyID = value;
            }
        }

        public event EventHandler DataInsert;

        public event EventHandler DataUpdate;

        public event EventHandler DataDelete;

        public event EventHandler DataSelectSingle;

        #endregion
    }
}
